<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>多种语言连接mysql数据库 | 个人文档</title>
    <meta name="generator" content="VuePress 1.8.2">
    
    <meta name="description" content="练习文档">
    
    <link rel="preload" href="/mybook/assets/css/0.styles.d89b46bb.css" as="style"><link rel="preload" href="/mybook/assets/js/app.17367f93.js" as="script"><link rel="preload" href="/mybook/assets/js/2.0cc5fd8f.js" as="script"><link rel="preload" href="/mybook/assets/js/9.a5d94e25.js" as="script"><link rel="prefetch" href="/mybook/assets/js/10.0ab847e7.js"><link rel="prefetch" href="/mybook/assets/js/11.b5a55ccb.js"><link rel="prefetch" href="/mybook/assets/js/3.f9542c36.js"><link rel="prefetch" href="/mybook/assets/js/4.682dc1df.js"><link rel="prefetch" href="/mybook/assets/js/5.f403064b.js"><link rel="prefetch" href="/mybook/assets/js/6.0a6170cc.js"><link rel="prefetch" href="/mybook/assets/js/7.b7420e90.js"><link rel="prefetch" href="/mybook/assets/js/8.03ff30b2.js">
    <link rel="stylesheet" href="/mybook/assets/css/0.styles.d89b46bb.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/mybook/" class="home-link router-link-active"><!----> <span class="site-name">个人文档</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/mybook/" class="nav-link">
  Home
</a></div><div class="nav-item"><a href="/mybook/guide/" class="nav-link">
  Guide
</a></div><div class="nav-item"><a href="https://google.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  External
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Languages" class="dropdown-title"><span class="title">Languages</span> <span class="arrow down"></span></button> <button type="button" aria-label="Languages" class="mobile-dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/mybook/language/chinese.html" class="nav-link">
  Chinese
</a></li><li class="dropdown-item"><!----> <a href="/mybook/language/japanese.html" class="nav-link">
  Japanese
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="高级" class="dropdown-title"><span class="title">高级</span> <span class="arrow down"></span></button> <button type="button" aria-label="高级" class="mobile-dropdown-title"><span class="title">高级</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          算法
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/mybook/language/chinese.html" class="nav-link">
  冒泡
</a></li><li class="dropdown-subitem"><a href="/mybook/language/japanese.html" class="nav-link">
  快速
</a></li></ul></li><li class="dropdown-item"><h4>
          设计模式
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/mybook/language/chinese.html" class="nav-link">
  工厂
</a></li><li class="dropdown-subitem"><a href="/mybook/language/chinese.html" class="nav-link">
  单例
</a></li></ul></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/mybook/" class="nav-link">
  Home
</a></div><div class="nav-item"><a href="/mybook/guide/" class="nav-link">
  Guide
</a></div><div class="nav-item"><a href="https://google.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  External
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Languages" class="dropdown-title"><span class="title">Languages</span> <span class="arrow down"></span></button> <button type="button" aria-label="Languages" class="mobile-dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/mybook/language/chinese.html" class="nav-link">
  Chinese
</a></li><li class="dropdown-item"><!----> <a href="/mybook/language/japanese.html" class="nav-link">
  Japanese
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="高级" class="dropdown-title"><span class="title">高级</span> <span class="arrow down"></span></button> <button type="button" aria-label="高级" class="mobile-dropdown-title"><span class="title">高级</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          算法
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/mybook/language/chinese.html" class="nav-link">
  冒泡
</a></li><li class="dropdown-subitem"><a href="/mybook/language/japanese.html" class="nav-link">
  快速
</a></li></ul></li><li class="dropdown-item"><h4>
          设计模式
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/mybook/language/chinese.html" class="nav-link">
  工厂
</a></li><li class="dropdown-subitem"><a href="/mybook/language/chinese.html" class="nav-link">
  单例
</a></li></ul></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>多种语言连接mysql数据库</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#使用php连接mysql" class="sidebar-link">使用php连接mysql</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#使用java连接mysql" class="sidebar-link">使用java连接mysql</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#下载安装相关驱动jar包" class="sidebar-link">下载安装相关驱动jar包</a></li><li class="sidebar-sub-header"><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#使用idea-创建新项目并创建目录结构" class="sidebar-link">使用IDEA 创建新项目并创建目录结构</a></li><li class="sidebar-sub-header"><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#然后将驱动jar包导入到项目中" class="sidebar-link">然后将驱动jar包导入到项目中</a></li><li class="sidebar-sub-header"><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#连接数据库" class="sidebar-link">连接数据库</a></li></ul></li><li><a href="/mybook/myBlog/%E5%A4%9A%E7%A7%8D%E8%AF%AD%E8%A8%80%E8%BF%9E%E6%8E%A5mysql%E6%95%B0%E6%8D%AE%E5%BA%93.html#使用python连接mysql" class="sidebar-link">使用python连接mysql</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="多种语言连接mysql数据库"><a href="#多种语言连接mysql数据库" class="header-anchor">#</a> 多种语言连接mysql数据库</h1> <p>我使用这三种语言连接过数据库，这里做下记录。</p> <p>分别是<strong>php</strong>， <strong>java，</strong> <strong>python</strong>。</p> <p>如果和我一样使用的<strong>xampp</strong>集成开发环境，确保Apache个MySQL打开：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004221047377.png" alt="image-20201004221047377"></p> <p>确保知道自己连接数据库的密码，用户名，主机名，如果有问题可以看看我的另一篇博客**“Xampp集成开发环境使用mysql”**</p> <p>我想显示一些数据，所以我创建名叫 <strong>test</strong> 的数据库，然后创建一个表叫 <strong>table01</strong>。建表语句：</p> <div class="language-sql extra-class"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> table01<span class="token punctuation">(</span>
    tid <span class="token keyword">INT</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span><span class="token punctuation">,</span>
    tname <span class="token keyword">CHARACTER</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
    tpassword <span class="token keyword">CHARACTER</span><span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span>
<span class="token punctuation">)</span>
</code></pre></div><p>然后插入三条记录：</p> <div class="language-sql extra-class"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> table01 <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'user01'</span><span class="token punctuation">,</span> <span class="token string">'password01'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> table01 <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'user02'</span><span class="token punctuation">,</span> <span class="token string">'password02'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> table01 <span class="token keyword">VALUES</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'user03'</span><span class="token punctuation">,</span> <span class="token string">'password03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>table01表的内容：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005010909370.png" alt="image-20201005010909370"></p> <p>好了，基本的准备工作就做好了。</p> <h2 id="使用php连接mysql"><a href="#使用php连接mysql" class="header-anchor">#</a> 使用php连接mysql</h2> <p>找到xampp的安装目录的xampp\htdocs 文件夹</p> <blockquote><p>[!NOTE]</p> <p>在安装xampp时我把存放它的文件夹名称设置为xampp，所以目录就是xampp\htdocs。在htdocs文件夹下也有一个叫xampp的文件夹，不要搞混了。</p></blockquote> <p>在htdocs 文件夹中新建一个文件夹，取个名字，我取名testphp，然后在testphp文件夹中新建一个文本文档，修改文件名和后缀为：<strong>index.php</strong></p> <p>我在<strong>HBuilderX</strong>编辑器中打开的结构为：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004233558788.png" alt="image-20201004233558788"></p> <p>然后修改index.php中的内容如下，全选复制进去保存。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>zh<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>viewport<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>width=device-width, initial-scale=1.0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>X-UA-Compatible<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>ie=edge<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
        <span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
            <span class="token comment">//变量声明</span>
            <span class="token variable">$username</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;cbz&quot;</span><span class="token punctuation">;</span>       <span class="token comment">//用户名</span>
            <span class="token variable">$password</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;2zUT6X2OYQUqONaO&quot;</span><span class="token punctuation">;</span>    <span class="token comment">//生成的密码</span>
            <span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;localhost&quot;</span><span class="token punctuation">;</span>     <span class="token comment">//主机名称</span>

            <span class="token variable">$dbname</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;test&quot;</span><span class="token punctuation">;</span>    	<span class="token comment">//数据库名称</span>
            <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;&quot;</span><span class="token punctuation">;</span>   		 <span class="token comment">//sql 语句，使用时赋值</span>
            <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">;</span>   	 <span class="token comment">//用于获取每次查询的结果</span>

            <span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">mysqli</span><span class="token punctuation">(</span><span class="token variable">$servername</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//连接数据库，返回 连接对象</span>

            <span class="token comment">//1.判断是否连接成功</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token operator">-&gt;</span><span class="token property">connect_error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token string double-quoted-string">&quot;连接失败: &quot;</span> <span class="token operator">.</span> <span class="token variable">$conn</span><span class="token operator">-&gt;</span><span class="token property">connect_error</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//连接失败输出错误信息</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">else</span><span class="token punctuation">{</span>
                <span class="token keyword">echo</span> <span class="token string double-quoted-string">&quot;连接成功！&lt;/br&gt;&quot;</span><span class="token punctuation">;</span>     <span class="token comment">//否则输出连接成功  echo 是php中的一个输出函数，输出内容直接作为文档的一部分，&lt;/br&gt; 是html中的换行标签</span>
            <span class="token punctuation">}</span>

            <span class="token comment">//2.选择要进行操作的数据库</span>
            <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;show databases;&quot;</span><span class="token punctuation">;</span>    <span class="token comment">//sql语句，显示所有数据库</span>
            <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$conn</span><span class="token operator">-&gt;</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// 查询所有数据库</span>

            <span class="token comment">//3.如果 result 不为空，遍历，找到 我需要用到的数据库，并设置</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$result</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token variable">$obj</span> <span class="token operator">=</span> <span class="token variable">$result</span><span class="token operator">-&gt;</span><span class="token function">fetch_object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token property">Database</span> <span class="token operator">===</span> <span class="token variable">$dbname</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                        <span class="token keyword">echo</span> <span class="token punctuation">(</span><span class="token string double-quoted-string">&quot;有&quot;</span> <span class="token operator">.</span> <span class="token variable">$dbname</span> <span class="token operator">.</span> <span class="token string double-quoted-string">&quot;数据库!&lt;/br&gt;&lt;/br&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//php中 字符串 连接运算符 是 一个 点</span>
                        <span class="token variable">$conn</span><span class="token operator">-&gt;</span><span class="token function">select_db</span><span class="token punctuation">(</span><span class="token variable">$dbname</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  	<span class="token comment">//选择这个数据库,之后的操作都会在这个数据库里进行</span>
                        <span class="token keyword">break</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">else</span><span class="token punctuation">{</span>
                <span class="token keyword">echo</span> <span class="token string double-quoted-string">&quot;查询没有&quot;</span> <span class="token operator">.</span> <span class="token variable">$dbname</span> <span class="token operator">.</span> <span class="token string double-quoted-string">&quot;数据库!&lt;/br&gt;&quot;</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token comment">//4.查询 table01 表的所有内容并显示</span>
            <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;select * from table01&quot;</span><span class="token punctuation">;</span>     <span class="token comment">//sql 语句， 查询table01表的所有内容</span>
            <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$conn</span><span class="token operator">-&gt;</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 执行sql查询语句，获取table01表的所有内容</span>

            <span class="token comment">// 如果返回的内容不为空</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$result</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token comment">//如果返回的记录数大于0</span>
                <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token variable">$result</span><span class="token operator">-&gt;</span><span class="token property">num_rows</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                    <span class="token comment">//获取一条记录赋值给$row, 然后判断是否为空，不为空则输出记录内容</span>
                    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token variable">$row</span> <span class="token operator">=</span> <span class="token variable">$result</span><span class="token operator">-&gt;</span><span class="token function">fetch_assoc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token keyword">echo</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string double-quoted-string">&quot;tid&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>    <span class="token comment">//输出 tid 通过字段名输出一条记录相应值,这里的tid就是在建table01表时的tid字段，下同</span>
                        <span class="token keyword">echo</span> <span class="token string double-quoted-string">&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;</span><span class="token punctuation">;</span>   <span class="token comment">//&amp;nbsp; 在 html 中表示一个空格</span>

                        <span class="token keyword">echo</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string double-quoted-string">&quot;tname&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>   <span class="token comment">//输出 tname</span>
                        <span class="token keyword">echo</span> <span class="token string double-quoted-string">&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;</span><span class="token punctuation">;</span>

                        <span class="token keyword">echo</span> <span class="token variable">$row</span><span class="token punctuation">[</span><span class="token string double-quoted-string">&quot;tpassword&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>   <span class="token comment">//输出  tpassword</span>
                        <span class="token keyword">echo</span> <span class="token string double-quoted-string">&quot;&lt;/br&gt;&lt;/br&gt;&quot;</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token delimiter important">?&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><blockquote><p>[!ERROR]</p> <p>记住，一定要<strong>保存修改</strong>!!! 在我的编程经历中，踩了不少次坑都是因为没保存而看不到效果。会怀疑人生的0.0</p></blockquote> <p>然后打开网址，在地址栏输入：http://localhost/testphp</p> <p>因为我创建的文件夹名称叫testphp，所有这里是testphp，如果和我不一样，要改为自己的文件夹名称。</p> <p>敲回车后：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004234752952.png" alt="image-20201004234752952"></p> <h2 id="使用java连接mysql"><a href="#使用java连接mysql" class="header-anchor">#</a> 使用java连接mysql</h2> <p>这里先假设你已经安装配置好了java的jdk和相关环境变量。</p> <p>后面的代码及相关操作会在<strong>IDEA</strong> 这款java集成开发环境软件中进行。</p> <hr> <h3 id="下载安装相关驱动jar包"><a href="#下载安装相关驱动jar包" class="header-anchor">#</a> 下载安装相关驱动jar包</h3> <p>使用java是类似的步骤，不过首先要下载 mysql 提供的java驱动jar包</p> <p>下载地址：https://dev.mysql.com/downloads/</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004235151876.png" alt="image-20201004235151876"></p> <p>然后选择平台无关：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004235508125.png" alt="image-20201004235508125"></p> <p>然后会有两个下载，就是解压格式不一样，两个都行，选一个下载就可以。我选的第二个：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004235639774.png" alt="image-20201004235639774"></p> <p>有可能提示你登录，点击下面的，直接下载：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201004235752828.png" alt="image-20201004235752828"></p> <p>如果打开网址很慢，可以使用百度云盘下载：</p> <p>链接：https://pan.baidu.com/s/1KoSyb8CFzQZqClBAy7mxmw
提取码：oup4</p> <p>下载好后解压：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005001123063.png" alt="image-20201005001123063"></p> <p>只需要红色框的jar包，单独复制到别的地方，其他文件可以删除。</p> <h3 id="使用idea-创建新项目并创建目录结构"><a href="#使用idea-创建新项目并创建目录结构" class="header-anchor">#</a> 使用IDEA 创建新项目并创建目录结构</h3> <p>1.创建的新项目名称我取名叫<strong>mysqljava</strong></p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005001908028.png" alt="image-20201005001908028"></p> <p>2.鼠标选择项目根目录，右键，创建一个文件夹，取名<strong>libs</strong>  就叫libs 不要自己取其他名称。</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005002045812.png" alt="image-20201005002045812"></p> <p>3.然后再<strong>src</strong>文件夹，就是上面那个蓝色的文件夹，新建一个包，我取名sqltest</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005002220106.png" alt="image-20201005002220106"></p> <p>4.接着在sqltest包里新建一个类，我取名SqlConn：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005002737046.png" alt="image-20201005002737046"></p> <p>5.将之前下载的jar包，那个jar结尾的文件。<strong>拖动</strong>到<strong>libs</strong>文件夹里。</p> <p>目录结构：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005002939444.png" alt="image-20201005002939444"></p> <h3 id="然后将驱动jar包导入到项目中"><a href="#然后将驱动jar包导入到项目中" class="header-anchor">#</a> 然后将驱动jar包导入到项目中</h3> <p>点击菜单栏 File-&gt;</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005003153155.png" alt=""></p> <p>弹出窗口依次点击：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005003345373.png" alt="image-20201005003345373"></p> <p>然后找到项目下的libs的jar包，点击选择，然后确定：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005003524528.png" alt="image-20201005003524528"></p> <p>OK后，返回上一级再点击OK，这样jar包就成功导入到项目。</p> <p>目录结构：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005003637797.png" alt="image-20201005003637797"></p> <h3 id="连接数据库"><a href="#连接数据库" class="header-anchor">#</a> 连接数据库</h3> <p>双击<strong>SqlConn</strong>类，在里面写连接的代码：</p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">sqltest</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SqlConn</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token class-name">Connection</span> connection <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>   <span class="token comment">//连接对象</span>
        <span class="token class-name">Statement</span> statement <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>    <span class="token comment">//中间状态对象</span>

        <span class="token class-name">String</span> USER <span class="token operator">=</span> <span class="token string">&quot;cbz&quot;</span><span class="token punctuation">;</span>       <span class="token comment">//用户名</span>
        <span class="token class-name">String</span> PASS <span class="token operator">=</span> <span class="token string">&quot;2zUT6X2OYQUqONaO&quot;</span><span class="token punctuation">;</span>        <span class="token comment">//密码</span>

        <span class="token class-name">String</span> DB_NAME <span class="token operator">=</span> <span class="token string">&quot;test&quot;</span><span class="token punctuation">;</span>   <span class="token comment">//数据库名称，要修改为自己的数据库名称</span>
        <span class="token class-name">String</span> JDBC_DRIVER <span class="token operator">=</span> <span class="token string">&quot;com.mysql.cj.jdbc.Driver&quot;</span><span class="token punctuation">;</span>   <span class="token comment">//驱动器名词，无需修改</span>
        <span class="token class-name">String</span> DB_URL <span class="token operator">=</span> <span class="token string">&quot;jdbc:mysql://localhost:3306/&quot;</span> <span class="token operator">+</span> DB_NAME <span class="token operator">+</span> <span class="token string">&quot;?useSSL=false&amp;serverTimezone=UTC&quot;</span><span class="token punctuation">;</span>

        <span class="token comment">//注意 ！ 上面两行代码是MySQL8版本到以上，如果是不到8的版本改为下面两句:</span>
        <span class="token comment">//String JDBC_DRIVER = &quot;com.mysql.jdbc.Driver&quot;;</span>
        <span class="token comment">//String DB_URL = &quot;jdbc:mysql://localhost:3306/&quot; + DB_NAME;</span>

        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>JDBC_DRIVER<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//加载驱动</span>
            connection <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>DB_URL<span class="token punctuation">,</span>USER<span class="token punctuation">,</span>PASS<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//开始连接</span>

            statement <span class="token operator">=</span> connection<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> sql <span class="token operator">=</span> <span class="token string">&quot;select * from table01&quot;</span><span class="token punctuation">;</span>	 <span class="token comment">//SQL语句  ,查询test_table表，要修改为自己的表</span>
            <span class="token class-name">ResultSet</span> resultSet <span class="token operator">=</span> statement<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//执行查询</span>

            <span class="token comment">//遍历结果</span>
            <span class="token keyword">while</span><span class="token punctuation">(</span>resultSet<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">String</span> id <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;tid&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//通过字段名获得记录对应值</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//输出字段</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;   &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token class-name">String</span> name <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//通过下标获得记录对应值，下标从1开始</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//输出字段</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;   &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token class-name">String</span> password <span class="token operator">=</span> resultSet<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;tpassword&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//通过字段名获得记录对应值</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//输出字段</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//输出换行</span>
            <span class="token punctuation">}</span>

            resultSet<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">//先关闭结果集</span>
            statement<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//关闭Statement对象</span>
            connection<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//最后关闭连接</span>

        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ClassNotFoundException</span> <span class="token operator">|</span> <span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>运行截图：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005005226513.png" alt="image-20201005005226513"></p> <h2 id="使用python连接mysql"><a href="#使用python连接mysql" class="header-anchor">#</a> 使用python连接mysql</h2> <p>我使用的Python3.7的版本，默认你已经下载好了python并配置好了环境变量。</p> <p>我使用的开发环境工具为<strong>PyCharm</strong></p> <hr> <p>新建一个项目，我取名pysql</p> <p>然后在项目里面新建一个.py文件，取名sqltest。</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005005811264.png" alt="image-20201005005811264"></p> <p>项目结构：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005005714491.png" alt="image-20201005005714491"></p> <p>双击sqltest.py，写代码如下：</p> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> pymysql

<span class="token comment"># 变量声明</span>
user_name <span class="token operator">=</span> <span class="token string">&quot;cbz&quot;</span>  <span class="token comment"># 用户名</span>
pass_word <span class="token operator">=</span> <span class="token string">&quot;2zUT6X2OYQUqONaO&quot;</span>  <span class="token comment"># 生成的密码</span>
server_name <span class="token operator">=</span> <span class="token string">&quot;localhost&quot;</span>    <span class="token comment"># 主机名称</span>
db_name <span class="token operator">=</span> <span class="token string">&quot;test&quot;</span>   <span class="token comment"># 要查询的表所在的数据库名称</span>

<span class="token comment"># 与数据库的服务端建立连接</span>
conn <span class="token operator">=</span> pymysql<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>host<span class="token operator">=</span>server_name<span class="token punctuation">,</span> user<span class="token operator">=</span>user_name<span class="token punctuation">,</span> password<span class="token operator">=</span>pass_word<span class="token punctuation">,</span> database<span class="token operator">=</span>db_name<span class="token punctuation">,</span> charset<span class="token operator">=</span><span class="token string">'utf8'</span><span class="token punctuation">)</span>

sql <span class="token operator">=</span> <span class="token string">&quot;select * from table01&quot;</span>   <span class="token comment"># sql语句，查询整个table01表</span>

cursor <span class="token operator">=</span> conn<span class="token punctuation">.</span>cursor<span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment"># 使用 cursor() 方法创建一个游标对象 cursor</span>
cursor<span class="token punctuation">.</span>execute<span class="token punctuation">(</span>sql<span class="token punctuation">)</span>

<span class="token keyword">for</span> row <span class="token keyword">in</span> cursor<span class="token punctuation">.</span>fetchall<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>row<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>     <span class="token comment"># end=&quot;&quot; 表示不换行</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;   &quot;</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">print</span><span class="token punctuation">(</span>row<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;   &quot;</span><span class="token punctuation">,</span> end<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">print</span><span class="token punctuation">(</span>row<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    
    <span class="token comment"># print(row)    # 上面五行输出输出代码可以直接用这一行代替</span>
</code></pre></div><p>运行结果：</p> <p><img src="https://gitee.com/banjieming/img/raw/master/imgs/image-20201005010750077.png" alt="image-20201005010750077"></p></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="/mybook/assets/js/app.17367f93.js" defer></script><script src="/mybook/assets/js/2.0cc5fd8f.js" defer></script><script src="/mybook/assets/js/9.a5d94e25.js" defer></script>
  </body>
</html>
